package com.andoridtools.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.andoridtools.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xu on 2017/5/20.
 */

public class DataThreePickerDialog extends Dialog {

    private Params params;

    public DataThreePickerDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    private void setParams(Params params) {
        this.params = params;
    }


//    public void setSelection(String itemValue) {
//        if (params.years.size() > 0) {
//            int idx = params.years.indexOf(itemValue);
//            if (idx >= 0) {
//                params.initSelectionYear = idx;
//                params.loopYear.setCurrentItem(params.initSelectionYear);
//            }
//        }
//    }

    public interface OnDataSelectedListener {
        void onDatasSelected(String yearsValue, int yearsPosition, String monthValue, int monthsPosition, String day, int dayPosition);

        void onCancel();
    }

    private static final class Params {
        private boolean shadow = true;
        private boolean canCancel = true;
        private LoopView loopYear;
        private LoopView loopMonths;
        private LoopView loopDays;
        private String title;
        private String unit;
        private LoopListener yearLoopListener;
        private LoopListener monthLoopListener;
        private LoopListener dayLoopListener;
        private int initSelectionYear;
        private int initSelectionMonth;
        private int initSelectionDay;
        private OnDataSelectedListener callback;
        private final List<String> years = new ArrayList<>();
        private final List<String> months = new ArrayList<>();
        private final List<String> days = new ArrayList<>();
    }

    public static class Builder {
        private final Context context;
        private final Params params;

        public Builder(Context context) {
            this.context = context;
            params = new Params();
        }

        private final String getCurrYearValue() {
            return params.loopYear.getCurrentItemValue();
        }

        private final String getCurrMonthsValue() {
            return params.loopMonths.getCurrentItemValue();
        }

        private final String getCurrDaysValue() {
            return params.loopDays.getCurrentItemValue();
        }

        public Builder setYearData(List<String> dataList) {
            params.years.clear();
            params.years.addAll(dataList);
            return this;
        }

        public Builder setMonthsData(List<String> dataList) {
            params.months.clear();
            params.months.addAll(dataList);
            return this;
        }

        public Builder setDaysData(List<String> dataList) {
            params.days.clear();
            params.days.addAll(dataList);
            return this;
        }


        public Builder setYearLoopListener(LoopListener yearLoopListener) {
            params.yearLoopListener = yearLoopListener;
            return this;
        }

        public Builder setMonthLoopListener(LoopListener monthLoopListener) {
            params.monthLoopListener = monthLoopListener;
            return this;
        }

        public Builder setTitle(String title) {
            params.title = title;
            return this;
        }

        public Builder setUnit(String unit) {
            params.unit = unit;
            return this;
        }

        public Builder setSelectionYear(int selection) {
            params.initSelectionYear = selection;
            return this;
        }

        public Builder setSelectionMonth(int selection) {
            params.initSelectionMonth = selection;
            return this;
        }

        public Builder setSelectionDay(int selection) {
            params.initSelectionDay = selection;
            return this;
        }


        public Builder setOnDataSelectedListener(OnDataSelectedListener onDataSelectedListener) {
            params.callback = onDataSelectedListener;
            return this;
        }


        public DataThreePickerDialog create() {
            final DataThreePickerDialog dialog = new DataThreePickerDialog(context, params.shadow ? R.style.Theme_Light_NoTitle_Dialog : R.style.Theme_Light_NoTitle_NoShadow_Dialog);
            View view = LayoutInflater.from(context).inflate(R.layout.layout_picker_three_data, null);

            if (!TextUtils.isEmpty(params.title)) {
                TextView txTitle = (TextView) view.findViewById(R.id.tx_title);
                txTitle.setText(params.title);
                txTitle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                        params.callback.onCancel();
                    }
                });
            }
            if (!TextUtils.isEmpty(params.unit)) {
                TextView tx_unit_year = (TextView) view.findViewById(R.id.tx_unit_year);
                tx_unit_year.setText(params.unit);
            }

            final LoopView loopYear = (LoopView) view.findViewById(R.id.loop_year);
            loopYear.setArrayList(params.years);
            loopYear.setNotLoop();
            if (params.years.size() > 0) {
                loopYear.setCurrentItem(params.initSelectionYear);
            }

            final LoopView loopMonths = (LoopView) view.findViewById(R.id.loop_month);
            loopMonths.setArrayList(params.months);
            loopMonths.setNotLoop();
            if (params.months.size() > 0) {
                loopMonths.setCurrentItem(params.initSelectionMonth);
            }

            final LoopView loopDays = (LoopView) view.findViewById(R.id.loop_day);
            loopDays.setArrayList(params.days);
            loopDays.setNotLoop();
            if (params.days.size() > 0) {
                loopDays.setCurrentItem(params.initSelectionDay);
            }

            view.findViewById(R.id.tx_finish).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    params.callback.onDatasSelected(
                            getCurrYearValue(),
                            loopYear.getCurrentItem(),
                            getCurrMonthsValue(),
                            loopMonths.getCurrentItem(),
                            getCurrDaysValue(),
                            loopDays.getCurrentItem());
                }
            });

            Window win = dialog.getWindow();
            win.getDecorView().setPadding(0, 0, 0, 0);
            WindowManager.LayoutParams lp = win.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            win.setAttributes(lp);
            win.setGravity(Gravity.BOTTOM);
            win.setWindowAnimations(R.style.Animation_Bottom_Rising);

            dialog.setContentView(view);
            dialog.setCanceledOnTouchOutside(params.canCancel);
            dialog.setCancelable(params.canCancel);

            params.loopYear = loopYear;
            params.loopMonths = loopMonths;
            params.loopDays=loopDays;
            dialog.setParams(params);
            return dialog;
        }

    }
}
